<?php

namespace App\Http\Controllers\Home\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Finance as FinanceModel;
use Position;
use Finance;
use FinanceSchedule;
use Frame;
use Users;
use Tools;
use Auth;
use Illuminate\Support\Facades\DB;

class DataController extends Controller
{
    protected $passField = [
        'quality','receivable'
    ];
    protected $financeSchedulePassField = [
        'type','time','receipts','describe'
    ];

    public function recommend_list(Request $request)
    {
        $in = [];

        if (!Auth::user()->hasRole('admin')){

            $user = Auth::user()->load('frame');

            $member = Frame::getMember($user->frame->id);

            $member[] = Auth::id();

            $in = ['assign',$member];
        }

        $page = $request->input('page');

        $page_info = Tools::page_helper($page,config('base.home_limit.base_limit.render_limit'),config('base.home_limit.base_limit.limit'));

        $list = Position::getList($page_info['skip'],$page_info['limit'],[],$in);

        foreach ($list as $k => $v){
            Position::getResultCount($v,3);
            Position::getResultCount($v,8);
            Position::getResultCount($v,10);
            Position::getResultCount($v,12);
            Position::getResultCount($v,13);
        }

        $count = Position::getCount([],$in);

        $num = intval(ceil($count/config('base.home_limit.base_limit.limit')));

        return view('Home.User.Data.recommend_list',compact('list','page','num'));

    }

    public function finance_list(Request $request)
    {
        $in = [];

        if (!Auth::user()->hasRole('admin')){

            $user = Auth::user()->load('frame');

            $member = Frame::getMember($user->frame->id);

            $member[] = Auth::id();

            $in = ['assign',$member];
        }

        $page = $request->input('page');

        $page_info = Tools::page_helper($page,config('base.home_limit.base_limit.render_limit'),config('base.home_limit.base_limit.limit'));

        $list = Finance::getList($page_info['skip'],$page_info['limit'],[],$in);

        $count = Finance::getCount(['assign'=>Auth::id()],$in,true);

        $num = intval(ceil($count/config('base.home_limit.base_limit.limit')));

        return view('Home.User.Data.finance_list',compact('list','page','num'));

    }

    public function finance_edit(FinanceModel $obj){
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'quality' => 'nullable|string',
                'receivable' => 'nullable|string',
            ],[],[
                'quality'=>'保质期',
                'receivable'=>'应收金额',
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField)){
                    $obj->$key = $val;
                }
            }

            $res = $obj->save();
            //返回结果
            return ['code'=>1];
        }

        $obj->load(['resume','position','client','from','assign_user']);

        $obj->entry_time = date('Y-m-d',strtotime($obj->entry_time));
        $obj->resume_name = $obj->resume?$obj->resume->name:'';
        $obj->position_name = $obj->position?$obj->position->title:'';
        $obj->client_name = $obj->client?$obj->client->chinese_name:'';
        $obj->user_name = $obj->from?$obj->from->username:'';
        $from_type = $obj->from?$obj->from->type:0;
        switch ($from_type){
            case 1:
                $obj->from_type = '内部推荐 - ';
                break;
            case 2:
                $obj->from_type = '兼职推荐 - ';
                break;
            default:
                $obj->from_type = '自投';
        }
        $obj->assign_name = $obj->assign_user?$obj->assign_user->username:'';

        return view('Home.User.Data.finance_edit',compact('obj'));
    }

    public function add_finance_schedule(FinanceModel $obj){
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'type' => 'required|string',
                'time' => 'required|string',
                'receipts' => 'required|integer',
                'describe' => 'nullable|string'
            ],[],[
                'type'   => '款项类型',
                'time' => '款项时间',
                'receipts'   => '款项金额',
                'describe'   => '款项说明'
            ]);


            //数据库操作
            $data = [];
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->financeSchedulePassField)){
                    $data[$key] = $val;
                }
            }

            $obj->all_schedule()->create($data);

            //返回结果
            return ['code'=>1];
        }

        $obj->load('all_schedule');

        if (count($obj->all_schedule)){
            foreach($obj->all_schedule as $k => $item){
                switch ($item->type){
                    case 0:
                        $obj->all_schedule[$k]->type_anme = '扣款';
                        break;
                    case 1:
                        $obj->all_schedule[$k]->type_anme = '收款';
                        break;
                    default:
                        $obj->all_schedule[$k]->type_anme = '扣款';
                }
            }
        }

        $obj = Tools::check_permission($obj);

        return view('Home.User.Data.add_finance_schedule',compact('obj'));
    }

    public function ranking1_list(){

        $between = Tools::getBetween();

        $list = FinanceSchedule::getOrderReceipts($between);

        foreach ($between as $k => $item){

            $between[$k] = $item->toDateTimeString();

        }

        return view('Home.User.Data.Ranking.ranking1_list',compact('list','between'));
    }

    public function ranking2_list(){

        $between = Tools::getBetween();

        $list = Users::getDeliverCount($between,[],[3]);

        $list = $list->toArray();

        if (!empty($list)){

            foreach ($list as $k => $v){

                $tmp[$k] = $list[$k]['cv'];

            }

            array_multisort($tmp,SORT_DESC,$list);

        }

        foreach ($between as $k => $item){

            $between[$k] = $item->toDateTimeString();

        }

        return view('Home.User.Data.Ranking.ranking2_list',compact('list','between'));
    }

    public function ranking3_list(){

        $between = Tools::getBetween();

        $list = Users::getDeliverCount($between,[],[10]);

        $list = $list->toArray();

        if (!empty($list)){

            foreach ($list as $k => $v){

                $tmp[$k] = $list[$k]['10count'];

            }

            array_multisort($tmp,SORT_DESC,$list);

        }

        foreach ($between as $k => $item){

            $between[$k] = $item->toDateTimeString();

        }

        return view('Home.User.Data.Ranking.ranking3_list',compact('list','between'));
    }

    public function ranking4_list(){

        $between = Tools::getBetween();

        //筛选通过	一面通过	二面通过	接offer	拒offer	待入职	已入职
        $list = Users::getDeliverCount($between,[],[3,6,8,10,11,12,13]);

        $list = $list->toArray();

        if (!empty($list)){

            foreach ($list as $k => $v){

                $tmp[$k] = $list[$k]['6count']+$list[$k]['8count'];

            }

            array_multisort($tmp,SORT_DESC,$list);

        }

        foreach ($between as $k => $item){

            $between[$k] = $item->toDateTimeString();

        }
        
        return view('Home.User.Data.Ranking.ranking4_list',compact('list','between'));
    }

}